import React, { useState } from "react";
import Navbar from "../../compoents/Navbar";
import { useNavigate } from "react-router-dom";
import style from "./index.module.css";
import { Tabs, Button, Checkbox } from "antd-mobile";
import NameCard from "../../compoents/NameCard";
function Index() {
  const navigate = useNavigate();
  
  const list = [
    {
      id:Math.floor(Math.random() * 100),
      title: "202000101001598z",
      keshi: "门诊外一科",
      doctor: "刘医师",
      time: "2020-01-01 10:20:36",
      price: "520.13",
      status: "缴费明细",
    },
    {
      id:Math.floor(Math.random() * 100),
      title: "202000101001598z",
      keshi: "口腔科",
      doctor: "章医师",
      time: "2020-01-01 13:20:36",
      price: "120.13",
      status: "缴费明细",
    },
  ];

  // 状态管理
  const [selectedItems, setSelectedItems] = useState<boolean[]>([]);
  const [selectAll, setSelectAll] = useState(false);

  // 初始化选中状态数组
  React.useEffect(() => {
    setSelectedItems(new Array(list.length).fill(false));
  }, [list.length]);

  // 处理单个复选框变化
  const handleItemCheck = (index: number, checked: boolean) => {
    const newSelectedItems = [...selectedItems];
    newSelectedItems[index] = checked;
    setSelectedItems(newSelectedItems);
    
    // 检查是否所有项目都被选中
    setSelectAll(newSelectedItems.every(item => item));
  };

  // 处理全选/取消全选
  const handleSelectAll = (checked: boolean) => {
    setSelectAll(checked);
    setSelectedItems(new Array(list.length).fill(checked));
  };

  // 计算已选中的张数
  const selectedCount = selectedItems.filter(item => item).length;

  // 计算总金额
  const totalAmount = list.reduce((sum, item, index) => {
    return sum + (selectedItems[index] ? parseFloat(item.price) : 0);
  }, 0);

  const goPay = () => {
    navigate('/PayOk',{state:{list}});
  }
  
  return (
    <div className={style.container}>
      <Navbar showBack onBack={() => navigate(-1)} title="门诊缴费" />

      <div className={style.content}>
        <NameCard />

        <div className={style.tabs}>
          <Tabs>
            <Tabs.Tab title="待缴费" key="fruits">
              {list.map((item, index) => (
                <div key={index} className={style.item}>
                  <div className={style.itemLeft}>
                    <h3>登记号:{item.title}</h3>
                    <Checkbox 
                      checked={selectedItems[index] || false}
                      onChange={(checked) => handleItemCheck(index, checked)}
                    />
                  </div>

                  <p>就诊科室:{item.keshi}</p>
                  <p>开单医生:{item.doctor}</p>
                  <p>开单时间:{item.time}</p>
                  <div>
                    <p>开单金额:{item.price}</p>
                    <p>{item.status}</p>
                  </div>
                </div>
              ))}
            </Tabs.Tab>
            <Tabs.Tab title="已缴费" key="vegetables">
              2
            </Tabs.Tab>
          </Tabs>
        </div>
      </div>

      <div className={style.bottom}>
        <div className={style.bottomBox}>
          <div className={style.bottomLeft}>
            <span>
              <Checkbox 
                checked={selectAll}
                onChange={handleSelectAll}
              />
            </span>

            <div className={style.bottomRight}>
              <span>全选(已选{selectedCount}张)</span>
              <span>
                合计:<a>¥{totalAmount.toFixed(2)}</a>
              </span>
            </div>
          </div>

          <div>
            <Button onClick={()=>goPay()} color='primary' className={style.submitButton}>去缴费</Button>
          </div>
        </div>
      </div>
    </div>
  );
}

export default Index;
